#!/bin/bash
# set -e
dir_path=$(cd `dirname $0`; pwd)
cd $dir_path; pwd

# 解析@分割的入参
# kong部署包版本号@telegreaf部署包版本号@influxdb部署机器ip地址@cassandra所有节点逗号分隔的ip地址@kong所属数据中心名@cassandra所有数据中心@磁盘挂载路径
CONFIG_PARAMS=$1
CONFIG_ARRAY=(${CONFIG_PARAMS//@/ })

# 变量定义开始
# 本次发布版本号
RELEASE_VERSION=${CONFIG_ARRAY[0]}
TELEGRAF_VERSION=${CONFIG_ARRAY[1]}
INFLUX_IP=${CONFIG_ARRAY[2]}
CASSANDRA_POINTS=${CONFIG_ARRAY[3]}
CASSANDRA_LOCAL_DATACENTER=${CONFIG_ARRAY[4]}
CASSANDRA_DATA_CENTERS=${CONFIG_ARRAY[5]}
# 挂载目录
MOUNT_PATH=${CONFIG_ARRAY[6]}

# 域名映射 多个空格 分割
# CUSTOM_HOST="--add-host=interface.zjzwfw.gov.cn:10.171.0.188 --add-host=www.baidu.com:10.171.0.188"
CUSTOM_HOST="--add-host=interface.zjzwfw.gov.cn:59.202.38.178"

# 日志根目录
LOG_ROOT_PATH=$MOUNT_PATH/logs

# telegraf采集日志使用的influxdb url
INFLUXDB_URL=http://$INFLUX_IP:8086

CASSANDRA_WRITE_CONSISTENCY=LOCAL_QUORUM
CASSANDRA_LB_POLICY=RequestDCAwareRoundRobin
CASSANDRA_REPL_STRATEGY=NetworkTopologyStrategy

# 能力网关KONG
SPACEX_KONG_PORT=8001
SPACEX_KONG_SSL_PORT=8443
SPACEX_KONG_ADMIN_PORT=8002
SPACEX_KONG_ADMIN_SSL_PORT=8444


# kong日志插件路径（不能更改）
SPACEX_KONG_LOG_PATH=$LOG_ROOT_PATH/spacex-kong/

# kong日志错误路径（不能更改）
SPACEX_KONG_LOG_ERROR_PATH=$LOG_ROOT_PATH/spacex-access-error/

# kong容器日志路径（不能更改）
SPACEX_KONG_C_LOG_PATH=$LOG_ROOT_PATH/spacex-kong-container/


echo "开始给目录赋权$LOG_ROOT_PATH/spacex-kong/"
mkdir -p $LOG_ROOT_PATH/spacex-kong/
mkdir -p $LOG_ROOT_PATH/spacex-access-error/
chmod -R 777 $LOG_ROOT_PATH/spacex-kong/
chmod -R 777 $LOG_ROOT_PATH/spacex-access-error/
echo "目录赋权成功"

echo "开始给目录赋权$LOG_ROOT_PATH/spacex-kong-container/"
mkdir -p $LOG_ROOT_PATH/spacex-kong-container/
chmod -R 777 $LOG_ROOT_PATH/spacex-kong-container/
echo "目录赋权成功"

echo "停止并删除kong"
docker stop spacex-kong
docker rm spacex-kong
docker load < ../images/spacex-kong-$RELEASE_VERSION.tar
echo "停止并删除kong成功"

echo "开始安装kong"

docker run --name spacex-kong --privileged \
        --dns 114.114.114.114 \
        --restart=always -d \
        $CUSTOM_HOST \
        -v /etc/localtime:/etc/localtime \
        -v  $SPACEX_KONG_LOG_PATH:/opt/logs/spacex-kong/ \
        -v  $SPACEX_KONG_LOG_ERROR_PATH:/opt/logs/spacex-access-error/ \
        -v  $SPACEX_KONG_C_LOG_PATH:/usr/local/kong/logs/ \
        -e "KONG_TRUSTED_IPS=0.0.0.0/0,::/0" \
        -e "KONG_REAL_IP_RECURSIVE=on" \
        -e "KONG_REAL_IP_HEADER=X-Forwarded-For" \
        -e "KONG_DATABASE=cassandra" \
        -e "KONG_CASSANDRA_CONTACT_POINTS=${CASSANDRA_POINTS}" \
        -e "KONG_CASSANDRA_WRITE_CONSISTENCY=${CASSANDRA_WRITE_CONSISTENCY}" \
        -e "KONG_CASSANDRA_LB_POLICY=${CASSANDRA_LB_POLICY}" \
        -e "KONG_CASSANDRA_LOCAL_DATACENTER=${CASSANDRA_LOCAL_DATACENTER}" \
        -e "KONG_CASSANDRA_REPL_STRATEGY=${CASSANDRA_REPL_STRATEGY}" \
        -e "KONG_CASSANDRA_DATA_CENTERS=${CASSANDRA_DATA_CENTERS}" \
        -e "KONG_DB_UPDATE_PROPAGATION=5" \
        -e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl" \
        -e "KONG_SERVER_TOKENS=off" \
        -e "KONG_LATENCY_TOKENS=off" \
        -p $SPACEX_KONG_PORT:8000 \
        -p $SPACEX_KONG_SSL_PORT:8443 \
        -p $SPACEX_KONG_ADMIN_PORT:8001 \
        -p $SPACEX_KONG_ADMIN_SSL_PORT:8444 \
        yunjia/spacex-kong:$RELEASE_VERSION

echo "kong安装成功"


echo "停止并删除telegraf"
docker stop spacex-telegraf
docker rm spacex-telegraf
docker load < ../images/telegraf-$TELEGRAF_VERSION.tar
echo "停止并删除telegraf成功"

echo "开始安装telegraf"
echo $SPACEX_KONG_LOG_PATH
docker run --name spacex-telegraf \
        --restart=always -d \
        -v /etc/localtime:/etc/localtime \
        -v $SPACEX_KONG_LOG_PATH:/opt/logs/spacex-kong/ \
        -e influxdb_url="$INFLUXDB_URL" \
        yunjia/spacex-telegraf:$TELEGRAF_VERSION
echo "telegraf安装成功"

echo "开始拷贝cron定时任务执行文件root"
chmod 0644 root
/bin/cp -f root /var/spool/cron/root
echo "成功拷贝cron定时任务执行文件root"
